package io.renren.modules.sys.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.validator.ValidatorUtils;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.sys.entity.ChargestandardEntity;
import io.renren.modules.sys.service.ChargestandardService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2019-11-26 09:16:58
 */
@RestController
@RequestMapping("sys/chargestandard")
public class ChargestandardController {
    @Autowired
    private ChargestandardService chargestandardService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:chargestandard:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = chargestandardService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/alllist")
    @RequiresPermissions("sys:chargestandard:alllist")
    public R alllist(@RequestParam Map<String, Object> params){
        List<ChargestandardEntity> list = chargestandardService.list(new QueryWrapper<ChargestandardEntity>());
        JSONArray jsonArray = new JSONArray();
        if(list!=null&&list.size()>0){
            for(ChargestandardEntity entity:list){
                JSONObject json = new JSONObject();
                json.put("id",entity.getId());
                json.put("timename",entity.getTimename());
                json.put("timemoney",entity.getTimemoney());
                jsonArray.add(json);
            }
        }
        return R.ok().put("chargestandard", jsonArray);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:chargestandard:info")
    public R info(@PathVariable("id") Integer id){
        ChargestandardEntity chargestandard = chargestandardService.getById(id);

        return R.ok().put("chargestandard", chargestandard);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:chargestandard:save")
    public R save(@RequestBody String str){

        JSONObject json = JSONObject.fromObject(str);

        String datastr = json.getString("data");
        if(StringUtils.isNotBlank(datastr)){
            String[] array = datastr.split(",");
            if(array.length>0){
                for(String string:array){
                    String[] arr = string.split("-");
                    if(arr.length==2){
                        ChargestandardEntity chargestandard = new ChargestandardEntity();
                        chargestandard.setTimename(Integer.parseInt(arr[0]));
                        chargestandard.setTimemoney(Integer.parseInt(arr[1]));
                        chargestandardService.save(chargestandard);
                    }
                }
            }
        }

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:chargestandard:update")
    public R update(@RequestBody String str){
        JSONObject json = JSONObject.fromObject(str);

        String datastr = json.getString("data");
        String id = json.getString("id");
        ChargestandardEntity chargestandard = chargestandardService.getById(id);
        if(StringUtils.isNotBlank(datastr)){
            String[] array = datastr.split(",");
            if(array.length>0){
                for(String string:array){
                    String[] arr = string.split("-");
                    if(arr.length==2){

                        chargestandard.setTimename(Integer.parseInt(arr[0]));
                        chargestandard.setTimemoney(Integer.parseInt(arr[1]));
                        chargestandardService.saveOrUpdate(chargestandard);
                    }
                }
            }
        }
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:chargestandard:delete")
    public R delete(@RequestBody Integer[] ids){
        chargestandardService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
